package com.woniu.cms.operation.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.operation.core.entity.OperationBudget;
import com.woniu.cms.operation.core.service.IOperationBudgetService;
import com.woniu.cms.operation.core.vo.OperationBasicinfoVo;
import com.woniu.cms.operation.core.vo.OperationBudgetVo;
import com.woniu.common.api.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 形象工程表 前端控制器
 * </p>
 *
 * @author chenjiapeng
 * @since 2022-09-23
 */
@RestController
@RequestMapping("/operation-budget")
public class OperationBudgetController {


    @Autowired
    private IOperationBudgetService iOperationBudgetService;



    @ApiOperation("根据id删除形象工程量数据")
    @DeleteMapping("/{id}")
    public Result deleteClientCompetitor(@PathVariable Integer id){

        boolean b = iOperationBudgetService.removeById(id);

        return b? Result.ok() :Result.error();

    }

    @ApiOperation("批量删除形象工程量表")
    @DeleteMapping
    public Result deleteClientCompetitor(@RequestBody List<Integer> ids){
        //批量删除
        boolean b = iOperationBudgetService.removeBatchByIds(ids);
        return b? Result.ok() :Result.error();
    }


    /**/
    @ApiOperation("修改形象工程量表")
    @PutMapping
    public Result updateClientCompetitor(@RequestBody OperationBudget  operationBudget){

        QueryWrapper<OperationBudget> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("project_name",operationBudget.getProjectName());

        boolean update = iOperationBudgetService.update(operationBudget, queryWrapper);

        return update ? Result.ok() :Result.error();
    }

    @ApiOperation("保存形象工程量表")
    @PostMapping("savecc")
    public Result saveClientCompetitor(@RequestBody OperationBudget  operationBudget){

        boolean save = iOperationBudgetService.save(operationBudget);
        return save ? Result.ok() :Result.error();
    }

 /*   @ApiOperation("保存竞争对手基本信息")
    @PostMapping("saveByName")
    public Result saveClientCompetitorName(String bidcompetitorName){

        Result save = iOperationBudgetService.saveName(bidcompetitorName);

        return save;
    }*/


    @ApiOperation(value = "通过形象工程量表列表Id查找形象工程量对象")
    @GetMapping("{id}")
    public  Result findById(@PathVariable   Integer id) {

        OperationBudget operationBudget = iOperationBudgetService.getById(id);

        return Result.common(operationBudget);
    }

    @ApiOperation(value = "通过形象工程量表中的项目名称查找形象工程量表的详细信息")
    @GetMapping("isname")
    public  Result findByName(String projectName) {

        System.out.println("projectName = " + projectName);

        QueryWrapper<OperationBudget> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("project_name",projectName);

        OperationBudget operationBudget = iOperationBudgetService.getOne(queryWrapper);

        return  Result.common(operationBudget);

    }


    /*@ApiOperation(value = "通过客户姓名查找客户对象")
    @GetMapping("isname")
    public  void findIsByName(String bidcompetitorName) {

        System.out.println("bidcompetitorName = " + bidcompetitorName);

        QueryWrapper<OperationBudget> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("bidcompetitor_name",bidcompetitorName);

        ClientCompetitor clientCompetitor = iOperationBudgetService.getOne(queryWrapper);

        if (clientCompetitor == null){
            Result result = saveClientCompetitorName(bidcompetitorName);
        }else if (clientCompetitor!=null){
            return;
        }
    }*/

    @GetMapping("page")
    @ApiOperation(value = "形象工程量表分页的查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前的页数",example = "1"),
            @ApiImplicitParam(name = "pageSize",value = "分页大小",example = "3"),
    })
    public Result pageOperationBudget(Integer currentPage, Integer pageSize){

        System.out.println("currentPage = " + currentPage);
        System.out.println("pageSize = " + pageSize);


        IPage<OperationBudget> OperationBudgetIPage = new Page(currentPage,pageSize);

        IPage<OperationBudget> page = iOperationBudgetService.page(OperationBudgetIPage);

        System.out.println("page = " + page);

        List<OperationBudget> records = page.getRecords();

        System.out.println("records = " + records);

        long total = page.getTotal();

        System.out.println("total = " + total);


    /*    List<OperationBudgetVo> operationBudgetVos = copyListProperties(records);*/

        OperationBudgetVo operationBudgetVo = new OperationBudgetVo();


        List<OperationBudgetVo> operationBudgetVos = CommonBeanUtils.copyListProperties(records, OperationBudgetVo.class);



        Map<String,Object> map = new HashMap<>();
        map.put("data",operationBudgetVos);
        map.put("total",total);

        return Result.common(map);


    }


   /* private List<OperationBudgetVo>  copyListProperties(List<OperationBudget>  records){

        List<OperationBudgetVo> operationBudgetVos  = MyBeanUtils.copyListProperties(records, OperationBudgetVo::new);

        return operationBudgetVos;

    }
*/





}

